package vn.co.thn.android.videohot.fragment;

import java.util.ArrayList;

import com.android.volley.VolleyError;

import vn.co.thn.android.serviceconnect.callback.RequestCallBack;
import vn.co.thn.android.serviceconnect.request.Param;
import vn.co.thn.android.videohot.R;
import vn.co.thn.android.videohot.VHConstants;
import vn.co.thn.android.videohot.VHUrlApi;
import vn.co.thn.android.videohot.activity.VHMainActivity;
import vn.co.thn.android.videohot.activity.VHVideoDetailActivity;
import vn.co.thn.android.videohot.adapter.VHVideoListAdapter;
import vn.co.thn.android.videohot.common.VHCommonFragment;
import vn.co.thn.android.videohot.data.VHVideoItemData;
import vn.co.thn.android.videohot.json.VHJsonVideoList;
import vn.co.thn.android.videohot.view.PullAndLoadListView;
import vn.co.thn.android.videohot.view.PullAndLoadListView.OnLoadMoreListener;
import vn.co.thn.android.videohot.view.PullToRefreshListView.OnRefreshListener;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

public class VHVideoCategoryFragment extends VHCommonFragment implements OnItemClickListener, OnRefreshListener, OnLoadMoreListener {
	private PullAndLoadListView list_new;
	private VHVideoListAdapter adapter;
	private ArrayList<VHVideoItemData> mListData = new ArrayList<VHVideoItemData>();
	private int mPage = 0;
	private String mCategory_code;

	public static VHVideoCategoryFragment newInstance(String category_code) {
		VHVideoCategoryFragment fragment = new VHVideoCategoryFragment();
		Bundle data = new Bundle();
		data.putString(VHConstants.EXTRA_CATEGORY_ID, category_code);
		fragment.setArguments(data);
		return fragment;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		adapter = new VHVideoListAdapter(getActivity(), mListData);
		mPage = 0;
		mCategory_code = getArguments().getString(VHConstants.EXTRA_CATEGORY_ID);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.vh_video_category_fragment, container, false);
		list_new = (PullAndLoadListView) view.findViewById(R.id.lst);
		list_new.setOnItemClickListener(this);
		list_new.setOnRefreshListener(this);
		list_new.setOnLoadMoreListener(this);
		return view;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);
		list_new.setAdapter(adapter);
		LoadData();

	}

	private void LoadData() {
		mPage = 0;
		ArrayList<Param> params = new ArrayList<Param>();
		params.add(new Param("page", mPage));
		if (mCategory_code != null) {
			params.add(new Param("category_key", mCategory_code));
		}
		this.mRequestService.setUrl(VHUrlApi.Api_Video_Category);
		this.mRequestService.setParams(params);
		this.mRequestService.setRequestCallBack(new RequestCallBack<VHJsonVideoList>() {

			@Override
			public void onResult(VHJsonVideoList data, boolean isUpdate) {
				// TODO Auto-generated method stub
				mListData.clear();
				mListData.addAll(data.getVideo_list());
				adapter.notifyDataSetChanged();
				list_new.onRefreshComplete();
			}

			@Override
			public void onResult(ArrayList<VHJsonVideoList> data, boolean isUpdate) {
				// TODO Auto-generated method stub

			}

		});
		this.mRequestService.setNetWorkCallBack((VHMainActivity) getActivity());
		this.mRequestService.Post();

	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		VHVideoItemData itemData = (VHVideoItemData) list_new.getItemAtPosition(position);
		startActivity(new Intent(getActivity(), VHVideoDetailActivity.class).putExtra(VHConstants.EXTRA_VIDEO, itemData));
	}

	@Override
	public void onDataRefresh() {
		// TODO Auto-generated method stub
		LoadData();
	}

	@Override
	public void onRefresh() {
		ArrayList<Param> params = new ArrayList<Param>();
		mPage = 0;
		this.mRequestService.setParams(params);
		params.add(new Param("category_key", mCategory_code));
		this.mRequestService.setRequestCallBack(new RequestCallBack<VHJsonVideoList>() {

			@Override
			public void onResult(VHJsonVideoList data, boolean isUpdate) {
				// TODO Auto-generated method stub
				mListData.clear();
				mListData.addAll(data.getVideo_list());
				adapter.notifyDataSetChanged();
				list_new.onRefreshComplete();
			}

			@Override
			public void onResult(ArrayList<VHJsonVideoList> data, boolean isUpdate) {
				// TODO Auto-generated method stub

			}

		});
		this.mRequestService.setNetWorkCallBack((VHMainActivity) getActivity());
		this.mRequestService.Post();

	}

	@Override
	public void onLoadMore() {
		ArrayList<Param> params = new ArrayList<Param>();
		mPage = mPage + 1;
		params.add(new Param("page", mPage));
		this.mRequestService.setParams(params);
		params.add(new Param("category_key", mCategory_code));
		this.mRequestService.setRequestCallBack(new RequestCallBack<VHJsonVideoList>() {

			@Override
			public void onResult(VHJsonVideoList data, boolean isUpdate) {
				// TODO Auto-generated method stub
				if (data.getVideo_list().size() == 0) {
					mPage = mPage - 1;
				}
				mListData.addAll(data.getVideo_list());
				adapter.notifyDataSetChanged();
				list_new.onLoadMoreComplete();
				adapter.notifyDataSetChanged();
				list_new.onLoadMoreComplete();
			}

			@Override
			public void onResult(ArrayList<VHJsonVideoList> data, boolean isUpdate) {
				// TODO Auto-generated method stub

			}

		});
		this.mRequestService.setNetWorkCallBack((VHMainActivity) getActivity());
		this.mRequestService.Post();
	}

	@Override
	public void onError(VolleyError error) {
		// TODO Auto-generated method stub
		super.onError(error);
		list_new.onLoadMoreComplete();
		list_new.onLoadMoreComplete();
	}

	@Override
	public void onException(Exception e) {
		// TODO Auto-generated method stub
		super.onException(e);
		list_new.onLoadMoreComplete();
		list_new.onLoadMoreComplete();
	}
}
